package org.teziyou.ticket.model;

import java.util.List;

import org.apache.log4j.Logger;
import org.teziyou.ticket.MemcachedUtil;
import org.teziyou.ticket.util.CacheKeyUtil;

import com.jfinal.ext.plugin.sqlinxml.SqlKit;
import com.jfinal.plugin.activerecord.Model;

@SuppressWarnings("unchecked")
public class Dict extends Model<Dict> {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(Dict.class);

	/**
	 * 
	 */
	private static final long serialVersionUID = -7581749898320084033L;
	
	public static final Dict dao = new Dict();
	
	public Dict getDictById(int id) {
		logger.info("get dict from cache");
		String key = CacheKeyUtil.getDictKey(id);
		Object obj = MemcachedUtil.client.get(key);
		if(obj==null) {
			logger.info("cache is null, get from db");
			obj = Dict.dao.findById(id);
			MemcachedUtil.client.set(key, obj);
		}
		return (Dict)obj;
	}
	
	public List<Dict> getDictsByItemType(String itemType) {
		logger.info("get dict from cache");
		String key = CacheKeyUtil.getTypeDictKey(itemType);
		logger.info("key="+key);
		Object obj = MemcachedUtil.client.get(key);
		if(obj==null) {
			logger.info("cache is null, get from db");
			String sql = SqlKit.sql("dict.getDictsByItemType");
			obj = find(sql, itemType);
			MemcachedUtil.client.set(key, obj);
		}
		return (List<Dict>)obj;
	}
	
}
